package nadav.tasher.lightool.graphics.view.appview;

import nadav.tasher.lightool.ResourceTable;
import nadav.tasher.lightool.graphics.view.appview.corner.CornerView;
import nadav.tasher.lightool.graphics.view.appview.drawer.Drawer;
import ohos.aafwk.ability.AbilitySlice;
import ohos.agp.colors.RgbColor;

import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.ColorMatrix;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

public class AppView extends StackLayout {
    public static final int ORDER_NAVIGATION_ON_TOP = 0;
    public static final int ORDER_NAVIGATION_ON_BOTTOM = 1;
    private Window window;
    private Drawer drawer;
    private CornerView cornerView;
    private Scrolly scroll;
    private int order = ORDER_NAVIGATION_ON_TOP;
    private boolean drawNavigation = true, drawStatusbar = true;
    private Gradient backgroundColors = new Gradient(Color.WHITE.getValue());
    int oldY;
    int oldX ;

    public AppView(AbilitySlice abilitySlice) {
        super(abilitySlice);
        setWindow(abilitySlice.getWindow());
        init();
    }

    private void init(){
        drawer = new Drawer(getContext());
        scroll = new Scrolly(getContext());

        ScrollHelper scrollHelper = new ScrollHelper();

        cornerView = new CornerView(getContext());
        initOrder();
    }

    public void setOrder(int order) {
        this.order = order;
        initOrder();
    }

    private void initOrder(){
        removeAllComponents();
        addComponent(scroll);
        if (order == ORDER_NAVIGATION_ON_TOP){
            addComponent(drawer);
            addComponent(cornerView);
        }else {
            addComponent(cornerView);
            addComponent(drawer);
        }
    }
    public Drawer getDrawer() {
        return drawer;
    }

    public Scrolly getScrolly() {
        return scroll;
    }

    public CornerView getCornerView() {
        return cornerView;
    }

    public Gradient getBackgroundColor() {
        return backgroundColors;
    }

    public void setBackgroundColor(int color) {
        backgroundColors = new Gradient(color);
        updateColors();
    }

    private void updateColors(){
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setOrientation(ShapeElement.Orientation.TOP_TO_BOTTOM);
        shapeElement.setRgbColors(new RgbColor[]{
                RgbColor.fromArgbInt(backgroundColors.getColorTop()),
                RgbColor.fromArgbInt(backgroundColors.getColorBottom())
        });
        setBackground(shapeElement);

        if (window != null){
            setWindowColors(backgroundColors);
        }
    }

    private void setWindow(Window window){
        this.window = window;
        if (this.window != null) {
            setFlags();
            setWindowColors(backgroundColors);
        }
    }

    private void setFlags(){
        if (window != null){
            window.clearFlags(WindowManager.LayoutConfig.MARK_TRANSLUCENT_STATUS);
            window.clearFlags(WindowManager.LayoutConfig.MARK_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.addFlags(WindowManager.LayoutConfig.MARK_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }
    }

    private void setWindowColors(Gradient g){
        if (window != null){
            if (drawNavigation) window.setNavigationBarColor(g.colorBottom);
            if (drawStatusbar) window.setStatusBarColor(g.colorTop);
        }
    }

    public void setDrawStatusbar(boolean drawStatusbar) {
        this.drawStatusbar = drawStatusbar;
    }

    public void setDrawNavigation(boolean drawNavigation) {
        this.drawNavigation = drawNavigation;
    }

    public static class Gradient{
        private int colorTop, colorBottom;


        public Gradient(int color) {
            colorTop = color;
            colorBottom = color;
        }

        public Gradient(int colorTop, int colorBottom) {
            this.colorTop = colorTop;
            this.colorBottom = colorBottom;
        }


        public int getColorBottom() {
            return colorBottom;
        }

        public int getColorTop() {
            return colorTop;
        }
    }

    public static class Scrolly extends ScrollView {

        private OnScroll list;

        public Scrolly(Context context) {
            super(context);
            setScrollbarOverlapEnabled(false);
            enableScrollBar(VERTICAL, false);
            setLayoutConfig(new ComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT));
        }

        public void setView(Component c){
            super.removeAllComponents();
            super.addComponent(c);
        }

        public void setOnScroll(OnScroll os) {
            list = os;
        }

//        protected void onScrollChanged(int l, int t, int oldl, int oldt){
//            if (list!=null){
//                list.onScroll(1,t,oldl,oldt);
//            }
//        }


        public interface OnScroll {
            void onScroll(int l, int t, int ol, int ot);
        }
    }

}
